package com.manli.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.manli.api.base.annotation.Permissions;
import com.manli.api.pojo.QueryParamsArea1;
import com.manli.api.pojo.QueryParamsDataView;
import com.manli.api.pojo.QueryParamsDataViewWechat;
import com.manli.api.pojo.Result;
import com.manli.api.service.AAreasService;
import com.manli.api.service.DataViewService;
import com.manli.api.util.DateUtils;
import com.manli.api.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Set;


/**
 * 数据展示
 *
 * @author manli
 * @email
 * @date 2020-05-15 11:39:04
 */
@Api(value = "/dataview", tags = "数据展示")
@RestController
@RequestMapping("api/dataview")
public class DataViewController {
    @Autowired
    DataViewService dataViewService;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 获取所有商户定位数据
     */
    @ApiOperation(value = "获取所有商户定位数据", notes = "")
    @PostMapping("/queryBranchLocationAll")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result queryBranchLocationAll(@RequestBody QueryParamsDataView queryParamsDataView) {
        return dataViewService.queryBranchLocationAll(queryParamsDataView);
    }

    /**
     * 获取所有用户定位数据
     */
    @ApiOperation(value = "获取所有用户定位数据", notes = "分为当天 3天 5天 7天 半个月 1个月筛选时间")
    @PostMapping("/queryWechatUserLocatioAll")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result queryWechatUserLocatioAll(@RequestBody QueryParamsDataViewWechat queryParamsDataViewWechat) {
        return dataViewService.queryWechatUserLocatioAll(queryParamsDataViewWechat);
    }

    /**
     * 获取当前在线用户数量
     */
    @ApiOperation(value = "获取当前在线用户数量", notes = "")
    @RequestMapping("/wechatUserLoginCount")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_OPEN)
    public Result wechatUserLoginCount() {
        Set<String> backLoginKeys = redisTemplate.keys("auth_token:" + "*");
        Set<String> wechatLoginKeys = redisTemplate.keys("auth_token_wechat:" + "*");
        //获取昨日时间
        Date now = new Date();
        Date lastDay = DateTime.now().plusDays(-1).toDate();//昨天
        Date tommoryDay = DateTime.now().plusDays(1).toDate();//明天
        //查询登陆数量
        QueryParamsDataViewWechat queryParamsDataViewWechat = new QueryParamsDataViewWechat();
        queryParamsDataViewWechat.setWechatUserLocationStartCreateTime(DateUtils.format(lastDay,"yyyy-MM-dd"));
        queryParamsDataViewWechat.setWechatUserLocationEndCreateTime(DateUtils.format(now,"yyyy-MM-dd"));
        long yesterDayLoginCount = this.queryWechatUserLocatioAll(queryParamsDataViewWechat).getRows();
        //查询商户数量
        QueryParamsDataView queryParamsDataView = new QueryParamsDataView();
        long allBranchCount = dataViewService.queryBranchLocationAll(queryParamsDataView).getRows();
        queryParamsDataView.setBranchPartnerStartCreateTime(DateUtils.format(now,"yyyy-MM-dd"));
        queryParamsDataView.setBranchPartnerEndCreateTime(DateUtils.format(tommoryDay,"yyyy-MM-dd"));
        long newBranchCount = dataViewService.queryBranchLocationAll(queryParamsDataView).getRows();
        JSONObject object = new JSONObject();
        object.put("backLoginKeysCount", backLoginKeys.size());//实时后台登陆在线人数
        object.put("wechatLoginKeysCount",wechatLoginKeys.size());//实时微信端登陆在线人数（今日用户在线数）
        object.put("yesterDayLoginCount",yesterDayLoginCount);//昨日登陆数量
        object.put("allBranchCount",allBranchCount);//一共的商户数量（包含禁用和启用的）
        object.put("newBranchCount",newBranchCount);//今日新增的商户数量（包含禁用和启用的）

        return ResultUtil.success(object);
    }

    /**
     * 获取商户注册数量及在用数量，及主营分类
     */
    @ApiOperation(value = "获取商户注册数量及在用数量，及主营分类", notes = "")
    @RequestMapping("/queryBranchPartnerDataAndMajorType")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result queryBranchPartnerDataAndMajorType() {
        return dataViewService.queryBranchPartnerDataAndMajorType();
    }


    /**
     * 获取产品有多少，有多少上架
     */
    @ApiOperation(value = "获取产品有多少，有多少上架", notes = "")
    @RequestMapping("/queryGoodsModel")
    @Permissions(module = "", privilege = "", typeOfAccess = Permissions.METHOD_LOGIN)
    public Result queryGoodsModel() {
        return dataViewService.queryGoodsModel();
    }

}
